package file_watcher

import (
	"github.com/julienschmidt/httprouter"
	"github.com/tidwall/gjson"
	"os"
)

func DelFile(fw *FileWatcher,data string,ps httprouter.Params)(s string,err error){

	name := ps.ByName("name")

	err = fw.Del(name)
	if err != nil{
		return "",err
	}

	return `{success}`,nil
}

func AddFile(fw *FileWatcher,data string,ps httprouter.Params)(string,error){

	filepaths := gjson.Get(data, "filepaths")
	var paths = make([]string, len(filepaths.Array()))
	for n, name := range filepaths.Array() {
		paths[n] = name.String()
	}

	for _,filePath := range paths {
		fstat, err := os.Stat(filePath)
		if err != nil {
           return err.Error(),err
		}
		finfo := FileInfo{filePath, fstat.Name(), 0, int(fstat.Size())}
		err = fw.Add(&finfo)
		if err != nil {
			return err.Error(),err
		}
	}
	return `{success}`,nil
}